﻿<div class="page-body">
    <!--<a class="btn btn-default purple" href="{:U('export')}" style="margin-bottom: 15px;"><i class="glyphicon glyphicon-cloud-download"></i>-->
    <!--导出全部-->
    <!--</a>-->
    <div class="row">
        <div class="col-xs-12 col-md-12">
            <div class="widget">
                <div class="widget-header ">
                    <span class="widget-caption">订单列表</span>
                    <div class="widget-buttons">
                        <a href="#" data-toggle="maximize">
                            <i class="fa fa-expand"></i>
                        </a>
                        <a href="#" data-toggle="collapse">
                            <i class="fa fa-minus"></i>
                        </a>
                        <a href="#" data-toggle="dispose">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="widget-body">
                    <div class="table-toolbar">
                        <form method="get" action="{:U('index')}" class="form-inline" role="form">
                            <div class="form-group">
                                <span class="input-icon" style="width: 200px;">
                                    <input type="text" class="form-control input-sm" placeholder="ID（订单号）,会员卡号"
                                           name="keywords" value="{$Think.get.keywords}">
                                    <i class="glyphicon glyphicon-search blue"></i>
                                </span>
                                <div class="input-group">
                                    <span class="input-group-addon">
                                        <i class="fa fa-calendar"></i>
                                    </span>
                                    <input type="text" class="form-control active" name="interval_time" id="interval_time" value="{$Think.get.interval_time}">
                                </div>
                                <div class="input-group">
                                    类型:
                                    <select name="type">
                                        <option value="2" <eq name="Think.get.type" value="2">selected=selected</eq> >全部</option>
                                        <option value="0" <eq name="Think.get.type" value="0">selected=selected</eq> >买书</option>
                                        <option value="1" <eq name="Think.get.type" value="1">selected=selected</eq> >借书</option>
                                    </select>
                                </div>
                                <div class="input-group">
                                    订单来源:
                                    <select name="online">
                                        <option value="2" <eq name="Think.get.online" value="2">selected=selected</eq> >全部</option>
                                        <option value="0" <eq name="Think.get.online" value="0">selected=selected</eq> >微信订单</option>
                                        <option value="1" <eq name="Think.get.online" value="1">selected=selected</eq> >门店订单</option>
                                    </select>
                                </div>
                                <button type="submit" class="btn btn-info">搜索</button>
                                <button type="submit" class="btn btn-danger" name="execl" value="1">导出筛选</button>
                                日期搜索为订单创建时间
                            </div>
                        </form>
                    </div>

                    <table class="table table-striped table-hover">
                        <thead>
                        <tr>
                            <th>ID（订单号）</th>
                            <th>会员卡号</th>
                            <th>用户</th>
                            <th>类别</th>
                            <th>图书/数量</th>
                            <th>创建时间</th>
                            <th>支付</th>
                            <th>配送</th>
                            <th>破损回收</th>
                            <th>是否手机订单</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>
                        <volist name="data" id="vo">
                            <tr>
                                <td>{$vo.order_id}</td>
                                <td>{$vo.card_id}</td>
                                <td>
                                    <a href="{:U('User/index',['user_id'=>$vo['user']['user_id']])}">
                                        <a class="show_headimg">显示头像<img src="{$vo.user.headimgurl}" height="100"></a><br>
                                        {$vo.user.nickname}
                                    </a>
                                </td>
                                <td>
                                    <switch name="vo.type">
                                        <case value="0">买书</case>
                                        <case value="1">借书</case>
                                        <default/>
                                        {$vo.type}
                                    </switch>
                                </td>
                                <td>
                                    <volist name="vo.order_has_book" id="v">
                                        <a href="{:U('Book/index',['keywords'=>$v['spbs']])}">
                                            {$v.sm} * <br>
                                            数量:<span class="label label-primary">{$v.quantity}</span><br>
                                            条码：{$v.tm}<br>
                                        </a>
                                        是否归还：<switch name="v.is_backed">
                                        <case value="1">已归还</case>
                                        <case value="0">未归还</case>
                                        <default />default
                                    </switch><br>

                                        归还数量：{$v.num_backed}<br>
                                        破损数量：{$v.num_broken}<br>
                                        <br>
                                    </volist>
                                    <volist name="vo.product" id="item">
                                        <a >
                                           积分兑换： {$item.name}
                                        </a>
                                    </volist>
                                </td>
                                <td>{$vo.pay.create_time}</td>
                                <td>
                                    ID：{$vo.pay.pay_id}<br>
                                    支付方式：{$vo.pay.way}<br>
                                    金额：{$vo.pay.money}<br>
                                    支付状态：
                                    <span class="label label-warning">
                                        <switch name="vo.pay.status">
                                            <case value="-1">取消</case>
                                            <case value="0">未支付</case>
                                            <case value="1">支付成功</case>
                                            <case value="2">支付失败</case>
                                            <default />default
                                        </switch>
                                    </span>
                                </td>
                                <td>
                                    联系人：{$vo.address.name}<br>
                                    电话：{$vo.address.phone}<br>
                                    地址：{$vo.address.province} {$vo.address.city} {$vo.address.district}<br>
                                    {$vo.address.address}<br>
                                    <span class="label label-lightred">备注:{$vo.order_remark}</span>
                                </td>
                                <td>
                                    <volist name="vo.order_broken" id="v">
                                        书名：{$v.sm}<br>
                                        实际回收金额：<span class="label label-primary">{$v.money}</span>元<br>
                                        图书原价：{$v.money_original}元<br>
                                        <br>
                                    </volist>
                                </td>
                                <td>
                                    <eq name="vo.is_online" value="0">
                                        是
                                        <else/>
                                        不是
                                    </eq>
                                </td>
                                <td>
                                    <eq name="vo.is_refund" value="1">
                                        <a class="btn btn-x btn-warning">已退款</a>
                                        <else/>
                                        <switch name="vo.state_order">
                                            <case value="0"><a class="btn btn-xs">等待付款</a></case>
                                            <case value="1">
                                                <a class="btn btn-success btn-xs" href="{:U('add',array('order_id'=>$vo['order_id']))}">
                                                    <i class="fa fa-edit"></i> 发货
                                                </a>
                                                <eq name="vo.type" value="1">
                                                    <a class="btn btn-danger btn-xs modify-btn" href="javascript:void(0);" data-order-id="{$vo.order_id}">
                                                        <i class="fa fa-edit"></i> 修改订单
                                                    </a>
                                                    <else/>
                                                    <a class="btn btn-danger btn-xs modify-btn" href="/index/Refund/index/id/{$vo.order_id}/type/1">
                                                        <i class="fa fa-edit"></i> 退款
                                                    </a>
                                                </eq>
                                            </case>
                                            <case value="2">
                                                <a class="btn btn-success btn-xs" href="{:U('edit',array('order_id'=>$vo['order_id']))}">
                                                    <i class="fa fa-edit"></i> 修改发货信息
                                                </a>
                                                <eq name="vo.type" value="1">
                                                    <a class="btn btn-danger btn-xs modify-btn" href="javascript:void(0);" data-order-id="{$vo.order_id}">
                                                        <i class="fa fa-edit"></i> 修改订单
                                                    </a>
                                                </eq>
                                            </case>
                                            <case value="3"><a class="btn btn-xs">已签收</a></case>
                                            <default />
                                        </switch>
                                    </eq>
                                </td>
                            </tr>
                        </volist>
                        </tbody>
                    </table>
                    {$page}
                    <form class="form-inline" method="post">
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">设置页码</div>
                                <input class="form-control" type="text" placeholder="" name="p" value="{$Think.get.p}" style="width: 60px;">
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 模态框（Modal） -->
<div class="modal fade" id="modal-order-modify" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">×
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    修改订单？
                </h4>
            </div>
            <div class="modal-body">
                <input type="text" id="tm" name="tm" class="form-control" placeholder="条形码,isbn" value="" onkeydown="addBook()">
                <h5>当前订单号:<span class="order_id"></span></h5>
            </div>
            <div class="row">
                <div class="col-xs-10 col-lg-offset-1">
                    <ol class="dd-list order_book_list">

                    </ol>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary btn-ok">
                    确认操作
                </button>
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">取消
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<!-- 模态框（Modal） -->
<div class="modal fade" id="modal-book-selector" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">×
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    请选择图书？
                </h4>
            </div>
            <div class="modal-body">
                <h5>共<span class="booknum label label-azure"></span>本</h5>
                <select class="form-control" name="booklist">
                </select>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-primary btn-ok">
                    确认操作
                </button>
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">取消
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
<div class="modal fade" id="view_book_list">
    <li class="dd-item bordered-palegreen" data-bookid="{{book_id}}">
        <div class="dd-handle {{bg_color}}">
            <a class="btn btn-default btn-sm shiny icon-only danger" href="javascript:void(0);" onclick="delOrderbook(event)"><i class="fa fa-times "></i></a>
            条码:{{tm}}-书名:{{sm}}-定价:{{dj}}
            -数量:<input name="quantity" class="form-control" value="{{quantity}}" />
        </div>
    </li>
</div>
<script src="/admin/js/datetime/moment.js"></script>
<script src="/admin/js/datetime/daterangepicker.js"></script>
<script>

    $('#interval_time').daterangepicker({
        format: "YYYY-MM-DD",
        separator: "~",
        locale: {
            applyLabel: '确定',
            cancelLabel: '取消',
            fromLabel: '开始时间',
            toLabel: '结束时间'
        }
    });
    var $view_bookitem=$('#view_book_list').html();
    var $target_bookitem=$('#modal-order-modify .order_book_list');

    $('.modify-btn').click(function (event) {
        var $order_id=$(event.currentTarget).attr('data-order-id');
        $.post('{:U("getOrder")}',{order_id:$order_id},function(data){
            $target_bookitem.html('');
            if(data.status=='success'){
                $('#modal-order-modify .order_id').html(data.data.order_id);
                $.each(data.data.order_has_book,function(i,item){
                    doadd(item);
                })
            }
        },'json');
        //提交保存
        $('#modal-order-modify').modal('show');

    });

    $('#modal-order-modify .btn-ok').click(function () {
        //提交保存
        var $orderdata=collectOrder();
        if($orderdata.booklist.length==0){
            alert('订单至少有一本图书');
            return;
        }
        $.post('{:U("saveOrder")}',$orderdata,function(data){
            log(data);
            if(data.status=='success'){
                $('#modal-delete').modal('hide');
                alert(data.data);
                location.reload();
            }else{
                alert(data.data);
            }
        },'json');
    });

    //添加图书
    function addBook() {
        $("#tm").focus();
        var keyCode = null;

        if (event.which)
            keyCode = event.which;
        else if (event.keyCode)
            keyCode = event.keyCode;

        if (keyCode == 13) {

            var $tm = $("#tm").val();
            if ($.trim($tm) == "") {
                $("#tm").val("");
                $("#tm").focus();
                return;
            }
            $.post("{:U('Book/ob_search')}", {"tm": $tm}, function (data) {
                if (data.status == "error") {
                    alert(data.data );
                } else {
                    //调用模态框选择数据
                    getSelectBook(data);
                }
            }, "json");
        }
    }
    //显示图书搜索选择模态框
    function getSelectBook($data) {
        var $booklist = $('#modal-book-selector [name="booklist"]');
        data = $data;
        $booklist.html("");
        $.each(data.data, function (i, item) {
            if (item.order_id == null) {
                item.order_id = "";
            }
            $str = '<option value="' + i + '">' +
                'ID：' + item.order_id + '/' + item.book_id + '-' +
                '书名：' + item.sm + '-' +
                '作者：' + item.zyz + '-' +
                '定价：￥<span class="dj">' + item.dj + '</span>元&nbsp;&nbsp;' +
                '' +
                '</option>';
            $booklist.prepend($str);
        });
        $('#modal-book-selector .booknum').html(data.data.length);
        $('#modal-book-selector').modal('show');
        $('#modal-book-selector .btn-ok').click(function () {
            //选择后回调
            var $i = $booklist.val();
            if ($i == null) {
                return;
            }
            $('#modal-book-selector').modal('hide');
            $booklist.html("");
            //回调
            doadd(data.data[$i],true);
        });
        $booklist.keydown(function () {
            var keyCode = null;

            if (event.which)
                keyCode = event.which;
            else if (event.keyCode)
                keyCode = event.keyCode;

            if (keyCode == 13) {
                $('#modal-book-selector .btn-ok').click();
            }
        });
        $('#modal-book-selector').show();
        $booklist.focus();
    }

    //模态框数据选择后回调 || 初始化
    function doadd(item,is_add) {
        if(is_add==undefined){
            is_add=false;
        }
        var $view=$view_bookitem;
        $view=$view.replace(/{{book_id}}/g,item.book_id);
        $view=$view.replace(/{{tm}}/g,item.tm);
        $view=$view.replace(/{{sm}}/g,item.sm);
        $view=$view.replace(/{{dj}}/g,item.dj);
        if(item.quantity<0 || item.quantity==undefined){
            item.quantity=0;
        }
        $view=$view.replace(/{{quantity}}/g,item.quantity);
        if(is_add){
            item.bg_color='bg-warning';
        }else{
            item.bg_color='';
        }
        $view=$view.replace(/{{bg_color}}/g,item.bg_color);
        $target_bookitem.prepend($view);
    }
    //删除书本
    function delOrderbook(event){
        $(event.currentTarget).parent().parent().remove();
    }
    //搜集订单资料
    function collectOrder(){
        var $data={};
        $data.order_id=$('#modal-order-modify').find('.order_id').html();
        $data.booklist=[];
        $.each($target_bookitem.find('li'),function(i,item){
            var $item={};
            $item.book_id=$(item).attr('data-bookid');
            $item.quantity=$(item).find('[name="quantity"]').val();
            $data.booklist.push($item);
        });
        return $data;
    }

</script>